<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<style>


	.lunul {
		list-style: none;
	}
	.lunBo{
		text-align: center;
		position: relative;
	}
	.lunBo .banner .item{
		display: none;
	}
	.lunBo .banner .item.active{
		display: block;
	}
	.lunBo .banner .item a{
		border: none;
	}
	.lunul{
		position: absolute;
		overflow: hidden;
		bottom: 10px;

	}
	.lunul li{
		float: left;
		width: 20px;
		font:bold 14px/20px "微软雅黑";
		background-color: #000;
		color: #fff;
		text-align: center;
		cursor: default;
		border-radius: 15px;

	}
	.lunul li+li{
		margin-left: 10px;
	}
	.lunul li.active{
		background-color: #fff;
		color: red;
	}
</style>
<script>
    var basePath = "https://5151px.com";
    var search_keyWord = "";
    var selectID,action = null,fileName;

    $(function() {
        init();
        searchAction();
        bootstrapValidatorInit();
        submitAction();
    })

    function init(){

        var localObj = window.location;
        if(store.get('userRole')=="2"){
            $("#add").show();
            $("#del").show();
            $("#view").show();
            $("#teacherSearch").val(store.get('userRealName'));
            $("#teacherSearch").attr("disabled","disabled");
            searchTeacherCourse(store.get('userId'),"#lessonNameSearch");
		}else{
            $("#add").hide();
            $("#del").hide();
            $("#view").hide();
            searchCourse("#lessonNameSearch");
		}
        dataListInit();
    }
    // 搜索动作
    function searchAction() {
        $('#search_button').click(function() {
            search_keyWord = $('#search_input').val();
            tableRefresh();
        })
    }

    //搜索课程列表
	function searchCourse(courseName){

        $.ajax({
            url:"/onlineEdu/lesson/getLessonGroup",
            type: 'GET',
            cache: false,
            data:{},
            dataType:  "json",
            async:false,
            success: function (data) {
                var options = '<option value="">--请选择--</option>'+data.lessonGroup;
                $(courseName).html(options);
            }
        });
	}

	function searchTeacherCourse(teacherId, lessonName){

        $.ajax({
            url:"/onlineEdu/lesson/getTeacherLessonGroup",
            type: 'GET',
            cache: false,
            data:{
                teacherId:teacherId
			},
            dataType:  "json",
            async:false,
            success: function (data) {
                var options = data.teacherLessonGroup;
                var classList = data.classList;
                if(classList.length==0){
                    $('#add').hide();
				}else{
                    $('#add').show();
                    $("#lessonName").html(options);
                    $("#lessonClass").append("<option value=\"\">--请选择--</option>");  classList.forEach(function (t) {
                        $("#lessonClass").append("<option value="+t.lesson_class+">"+t.lesson_class+"</option>");
					})
                }
                $(lessonName).html(options);
            }
        });
	}

    // 分页查询参数
    function queryParams(params) {

        var temp = {
            limit : params.limit,
            offset : params.offset,
            lessonName :$('#lessonNameSearch').val(),
            lessonClass:$('#classSearch').val(),
            teacherName:$('#teacherSearch').val()
        }
        return temp;
    }

    // 表格初始化
    function dataListInit() {
        $('#dataList').bootstrapTable(
            {
                columns : [{
                    checkbox: true,
                    align: 'center'
                }, {
                    title: 'ID',
                    formatter: function (value, row, index) {
                        return index+1;
                    },
                    align: "center"
                },
                    {
                        field : 'lesson_name',
                        title : '课程名称'
                    },
                    {
                        field : 'lesson_class',
                        title : '年级'
                    },
                    {
                        field : 'teacher_name',
                        title : '上课教师'
                    },
                    {
                        field : 'homework_subject',
                        title : '作业描述'
                    },
                    {
                        field : 'create_time',
                        title : '上传时间'
                    },
                    {
                        field : 'operation',
                        title : '操作',
                        width: 160,
                        align: 'center',
                        valign: 'middle',
                        formatter : function(value, row, index) {
                            var a = '<button id="view" class="btn btn-info btn-sm view"><span>查看</span></button>';
                            var d = '<button id="del" class="btn btn-danger btn-sm delete"><span>删除</span></button>';
                            return a +' '+ d;
                        },
                        events : {
                            // 操作列中编辑按钮的动作
                            'click .view' : function(e, value, row, index) {
                                selectID = row.homework_id;
                                if (store.get('userRole') == "2") {
                                    rowEditOperation(row.homework_id);
                                }else{
                                    alert("无权限");
								}
                            },
                            'click .delete' : function(e,value, row, index) {
                                selectID = row.homework_id;
                                if (store.get('userRole') == "2") {
                                    $('#deleteWarning_modal').modal(
                                        'show');
                                    deleteAction();
                                }else{
                                    alert("无权限");
                                }
                            }
                        }
                    } ],
                url : '/onlineEdu/homework/getTeacherHomeWorkByPage',
                onLoadError:function(status){
                    handleAjaxError(status);
                },
                method : 'GET',
                queryParams : queryParams,
                sidePagination : "server",
                dataType : 'json',
                pagination : true,
                pageNumber : 1,
                pageSize: 10,
                pageList : [5, 10, 25, 50, 100 ],
                clickToSelect : true
            });
    }

    // 表格刷新
    function tableRefresh() {
        $('#dataList').bootstrapTable('refresh', {
            query : {}
        });
    }
    //上传
    var uploading = false;
    function uploadFile(file, fileID) {
        if (uploading) {
            alert("文件正在上传中，请稍候");
            return false;
        }
        var form_data = new FormData();
        var file_data = $("#"+fileID).prop("files")[0];
        form_data.append("file",file_data);
        $.ajax({
            url:"/onlineEdu/homework/teacherHomeWorkUpload",
            type: 'POST',
            cache: false,
            data:form_data,
            processData: false,
            contentType: false,
            dataType:  "json",
            beforeSend: function () {
                uploading = true;
            },
            success: function (data) {
                fileName += data.fileName+",";
                alert("上传成功"+fileName);
                uploading = false;
            }
        });
    }
    // 行编辑操作
    function rowEditOperation(homeworkId) {

        $('#view_modal').modal("show");
        var datas = null;
        $.ajax({
            type : "GET",
            url :"/onlineEdu/homework/teacherHomeworkUrl",
            dataType : "json",
            data : {homeworkId:homeworkId},
            success : function(response) {
                datas = response.homeworkUrls;
                showHomeWork(datas);
            },
            error : function(xhr, textStatus, errorThrown) {
                $('#view_modal').modal("hide");
                alert("获取图片失败!");
                handleAjaxError(xhr.status);
            }
        })
    }

    function showHomeWork(datas){

        var banner = document.getElementById("banner");
        var list = document.getElementById("list");
        $("#banner").empty();
        $("#list").empty();
        for(var i = 0,len = datas.length;i<len;i++){
            var div = document.createElement("div");
            var li = document.createElement("li");
            if(i==0){  //默认第一项轮播项显示  对应的控制按钮被选中
                div.className = "item active";
                li.className = "active";
            }else{ //其他项隐藏  其他的控制按钮样式不改变
                div.className = "item";
                li.className = "";
            }

            div.innerHTML = '<a target="_blank" href="' + basePath+datas[i].homework_url + '">' +
                '<img width="550px" height="600px" src="' + basePath+datas[i].homework_url + '"/>' +
                '</a>';
            li.innerHTML = i + 1;
            banner.appendChild(div);
            list.appendChild(li);
        }

        var lunBo = document.getElementById("lunBo");
        var items = document.querySelectorAll("#lunBo #banner .item");
        var lis = document.querySelectorAll("#lunBo #list li");
        var currentIndex = 0;//(控制按钮和轮播项共同的索引)
        for(var i = 0,len = lis.length;i<len;i++){
            lis[i].index = i;
            lis[i].onmouseenter = function(){
                currentIndex = this.index;
                for(var j = 0;j<len;j++){
                    lis[j].className = "";
                    items[j].className = "item";
                }
                this.className = "active";
                items[this.index].className = "item active";
            }
        }

        var termId;
        function autoPlay(){
            termId = setInterval(function(){
                currentIndex++;
                if(currentIndex==lis.length){
                    currentIndex = 0;
                }
                lis[currentIndex].onmouseenter();
            },4000);
        }
        autoPlay();
        //鼠标进入停止轮播
        lunBo.onmouseenter = function(){
            clearInterval(termId);
        }
        //鼠标离开继续轮播
        lunBo.onmouseleave = function(){
            autoPlay();
        }
    }
    // 刪除信息
    function deleteAction(){
        $('#delete_confirm').click(function(){
            var data = {"homeWorkId" : "'"+selectID+"'"};
            deleteData(data);
        })
    }

    function deleteData(data) {

        $.ajax({
            type : "GET",
            url : "/onlineEdu/homework/delete",
            dataType : "json",
            contentType : "application/json",
            data : data,
            success : function(response){
                $('#deleteWarning_modal').modal("hide");
                var type;
                var msg;
                var append = '';
                if(response.success){
                    type = "success";
                    msg = "删除成功";
                }else{
                    type = "error";
                    msg = "删除失败";
                }
                showMsg(type, msg, append);
                tableRefresh();
            },
            error : function(response){
                $('#deleteWarning_modal').modal("hide");
                // handle error
                alert("失败");
            }
        })
        $('#deleteWarning_modal').modal('hide');
    }
    // 添加信息
    function submitAction() {

        $('#add').click(function() {
            $('#add_modal').modal("show");
            $('#titleLable').html("添加数据");
            $('#modal_cancel').show();
            $('#modal_submit').show();
            action = 'add';
        });

        $('#modal_submit').click(function() {

            var homworkUrl = fileName.replace("undefined","");
            var data = {
                lessonName:$('#lessonName').val(),
                lessonClass:$('#lessonClass').val(),
                teacherId:store.get('userId'),
                subject:$('#subject').val(),
                homworkUrl:homworkUrl.substring(0,fileName.length-1)
            }
            if(!(data.lessonName&&data.lessonClass&&data.subject)) {
                alert("请填写完整！");
                return ;
            }
            if(!(data.homworkUrl)) {
                alert("请上传作业图片！");
                return ;
            }
            $.ajax({
                type : "POST",
                url :"/onlineEdu/homework/save",
                dataType : "json",
                data : data,
                success : function(response) {
                    $('#add_modal').modal("hide");
                    var msg;
                    var type;
                    var append = '';
                    if (response.success) {
                        type = "success";
                        msg = "操作成功";
                    } else {
                        type = "error";
                        msg = "失败";
                    }
                    showMsg(type, msg, append);
                    tableRefresh();
                    $('#lessonName').val("");
                    $('#lessonClass').val("");
                    $('#subject').val("");
                },
                error : function(xhr, textStatus, errorThrown) {
                    $('#add_modal').modal("hide");
                    // handle error
                    handleAjaxError(xhr.status);
                }
            })
        })
    }
</script>
<div class="panel panel-default">
	<ol class="breadcrumb">
		<li><label class="form-label">作业信息</label></li>
	</ol>
	<div class="panel-body">
		<div class="row">
			<div style="width: 100%;margin-top: -16px" class="box-content">
				<table id="信息查询" class="table table-bordered table-hover table-responsive">
					<tbody>
					<tr style="display: none">
						<td class="item-title" align="center">上课教师</td>
						<td class="item-content-half">
							<input type="text" class="form-control" id="teacherSearch" placeholder="">
						</td>
					</tr>
					<tr>
						<td class="item-title" align="center">课程名称</td>
						<td class="item-content-half">
							<%--<input type="text" class="form-control" id="lessonNameSearch" placeholder="">--%>
							<select id="lessonNameSearch" class="form-control">
							</select>
						</td>
						<td class="item-title" align="center">年级</td>
						<td class="item-content-half">
							<select id="classSearch" class="form-control">
								<option value="">--请选择--</option>
								<option value="小班">小班</option>
								<option value="小升中">小升中</option>
								<option value="中班">中班</option>
								<option value="中升大">中升大</option>
								<option value="大班">大班</option>
								<option value="大升一">大升一</option>
								<option value="一年级">一年级</option>
								<option value="一升二">一升二</option>
								<option value="二年级">二年级</option>
								<option value="二升三">二升三</option>
								<option value="三年级">三年级</option>
								<option value="三升四">三升四</option>
								<option value="四年级">四年级</option>
								<option value="四升五">四升五</option>
								<option value="五年级">五年级</option>
								<option value="小升初">小升初</option>
							</select>
						</td>


					</tr>
					</tbody>
				</table>
			</div>
			<%--</div>--%>
		</div>
		<div style="text-align: right">
			<button id="search_button" style="text-align: right" class="btn btn-success">
				<span class="glyphicon glyphicon-search"></span> <span>查询</span>
			</button>
		</div>
	</div>
	<div class="row" style="margin-top: 25px">
		<div class="col-md-5">
			<button class="btn btn-sm btn-default" id="add">
				<span class="glyphicon glyphicon-plus"></span> <span>添加作业</span>
			</button>
		</div>
		<div class="col-md-5"></div>
	</div>
	<div class="row" style="margin-top: 15px">
		<div class="col-md-12">
			<table id="dataList" class="table table-striped"></table>
		</div>
	</div>
</div>
</div>

<!-- 浏览作业模态框 -->
<div id="view_modal" class="modal fade" table-index="-1" role="dialog"
	 aria-labelledby="myModalLabel" aria-hidden="true"
	 data-backdrop="static">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button class="close" type="button" data-dismiss="modal"
						aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="titleLablea">浏览作业</h4>
			</div>
			<div class="modal-body">
				<!-- 模态框的内容 -->
				<div class="lunBo" id="lunBo">
					<div class="banner" id="banner">
					</div>
					<ul class="lunul" id="list">
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>
<!-- 添加信息模态框 -->
<div id="add_modal" class="modal fade" table-index="-1" role="dialog"
	 aria-labelledby="myModalLabel" aria-hidden="true"
	 data-backdrop="static">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button class="close" type="button" data-dismiss="modal"
						aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="titleLable">添加信息</h4>
			</div>
			<div class="modal-body">
				<!-- 模态框的内容 -->
				<div class="row">
					<div class="col-md-1 col-sm-1"></div>
					<div class="col-md-8 col-sm-8">
						<form class="form-horizontal" role="form" id="student_form"
							  style="margin-top: 25px">
							<div class="form-group">
								<label  class="control-label col-md-4 col-sm-4"> <span>课程名称：</span>
								</label>
								<div class="col-md-8 col-sm-8">
										<select id="lessonName" class="form-control"></select>
								</div>
							</div>
							<div class="form-group">
								<label  class="control-label col-md-4 col-sm-4"> <span>年级：</span>
								</label>
								<div class="col-md-8 col-sm-8">
										<select id="lessonClass" class="form-control">
										</select>
								</div>
							</div>
							<div class="form-group">
								<label  class="control-label col-md-4 col-sm-4"> <span>作业描述：</span>
								</label>
								<div class="col-md-8 col-sm-8">
									<input type="text" class="form-control" id="subject"
										   placeholder="作业描述">
								</div>
							</div>
							<div class="form-group">
								<label  class="control-label col-md-4 col-sm-4"> <span>上传作业：</span>
								</label>
								<div class="col-md-8 col-sm-8">
									<form id = "fileForm" method="post" enctype="multipart/form-data">
										<input type="file" name="file" id="submitFile" onchange="uploadFile(this, this.id)">
									</form>
								</div>
							</div>
						</form>
					</div>
					<div class="col-md-1 col-sm-1"></div>
				</div>
			</div>
			<div class="modal-footer">
				<button class="btn btn-default" type="button" data-dismiss="modal" id="modal_cancel">
					<span>取消</span>
				</button>
				<button class="btn btn-success" type="button" id="modal_submit">
					<span>提交</span>
				</button>
			</div>
		</div>
	</div>
</div>

<!-- 删除提示模态框 -->
<div class="modal fade" id="deleteWarning_modal" table-index="-1"
	 role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button class="close" type="button" data-dismiss="modal"
						aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="myModalLabelee">警告</h4>
			</div>
			<div class="modal-body">
				<div class="row">
					<div class="col-md-3 col-sm-3" style="text-align: center;">
						<img src="" alt=""
							 style="width: 70px; height: 70px; margin-top: 20px;">
					</div>
					<div class="col-md-8 col-sm-8">
						<h3>是否确认删除该条记录</h3>
						<p></p>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button class="btn btn-default" type="button" data-dismiss="modal">
					<span>取消</span>
				</button>
				<button class="btn btn-danger" type="button" id="delete_confirm")>
					<span>确认删除</span>
				</button>
			</div>
		</div>
	</div>
</div>